#include <algorithm>
#include <ctime>
#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_ST_TIME = 1000;
const int MIN_ST_TIME = 0;
const int MAX_NEED_TIME = 100;
const int MIN_NEED_TIME = 10;

// define work
struct Work {
  int id;         // 作业ID，
  float st, need; // 到达时刻，需要花费时间
  int start_time, end_time;
  float rr; // 响应比
  bool ok;  // 是否完成
  bool in_wait;
  friend ostream &operator<<(ostream &out, const Work &w) {
    out << w.id << " " << w.st << " " << w.need << endl;
    return out;
  }
  bool operator<(const Work &w) const { return st < w.st; }
};

vector<Work *> qwait;
vector<Work *> worklist;
struct WorkResult {
  char name;
  float ht, st, ed, turn,
      turn_w; //到达时刻, 开始时刻， 结束时刻， 周转时间， 带权周转时间
};

void datacreate(int n) {
  fstream f("job", ios::out);
  vector<Work> jobs;
  f << n << endl;
  for (int i = 1; i <= n; i++) {
    Work w = {.id = i,
              .st = float(rand() % (MAX_ST_TIME - MIN_ST_TIME) + MIN_ST_TIME),
              .need = float(rand() % (MAX_NEED_TIME - MIN_NEED_TIME) +
                            MIN_NEED_TIME)};
    jobs.push_back(w);
  }
  sort(jobs.begin(), jobs.end());
  for (auto x : jobs) {
    f << x;
  }
  f.close();
}

void init(int n) { datacreate(n); }

int time_counter = 0;
void fcfs(Work *a) {
  a->start_time = time_counter;
  time_counter += a->need;
  a->end_time = time_counter;
  a->ok = true;
}
void sjf() {
  Work *t = nullptr;
  for (auto x : qwait) {
    if (x->ok)
      continue;
    if (t == nullptr || t->need > x->need)
      t = x;
  }
  if (t == nullptr)
    return;
  cout << "选择了" << t->id << " " << t->need << endl;
  t->start_time = time_counter;
  time_counter += t->need;
  t->end_time = time_counter;
  t->ok = true;
}
void hrrn() {
  Work *t = nullptr;
  for (auto x : qwait) {
    if (x->ok)
      continue;
    if (t == nullptr)
      t = x;
    x->rr = (time_counter - x->st) / x->need + 1;
  }
  for (auto x : qwait) {
    if (x->ok)
      continue;
    if (t->rr < x->rr)
      t = x;
  }
  if (t == nullptr)
    return;
  cout << "选择了" << t->id << " " << t->need << endl;
  t->start_time = time_counter;
  time_counter += t->need;
  t->end_time = time_counter;
  t->ok = true;
}

const char *DATA_FILE = "job";
const bool flag_fcfs = false;
int main(int argc, char *argv[]) {
  // init(20);
  fstream f(DATA_FILE, ios::in);
  int n;
  f >> n;

  while (n--) {
    Work *a = new Work{};
    f >> a->id, f >> a->st, f >> a->need;
    worklist.push_back(a);
  }
  for (; time_counter < 9000; time_counter++) {
    for (auto x : worklist) {
      if (x->ok)
        continue;
      if (x->st <= time_counter) {
        if (flag_fcfs) {
          fcfs(x);
          break;
        } else {
          if (x->in_wait)
            continue;
          qwait.push_back(x);
          cout << "sjf 日志打印" << endl;
          cout << "当前时间 " << time_counter << endl;
          cout << "start" << endl;
          for (auto x : qwait) {
            cout << x->id << " " << x->ok << " " << x->st << endl;
          }
          cout << "end" << endl;
          x->in_wait = true;
        }
      }
    }
    hrrn();
  }
  fstream r("result-hrrn", ios::out);
  double t_sum, tw_sum = 0;
  if (flag_fcfs) {
    for (auto x : worklist) {
      int turn = x->end_time - x->st;
      double w_turn = turn / x->need;
      t_sum += turn, tw_sum += w_turn;
      r << x->id << " " << x->start_time << " " << x->end_time << " " << x->st << " " << x->need << " " <<
        " " << x->end_time - x->st << " " << (x->end_time - x->st) / x->need<<endl;
    }
  }
  else {
    for (auto x : qwait) {
      int turn = x->end_time - x->st;
      double w_turn = turn / x->need;
      t_sum += turn, tw_sum += w_turn;
      r << x->id << " " << x->start_time << " " << x->end_time << " " << x->st << " " << x->need << " " <<
        " " << x->end_time - x->st << " " << (x->end_time - x->st) / x->need<<endl;
    }
  }
  r << t_sum / worklist.size() << " " << tw_sum / worklist.size() << " " << endl;
  return 0;
}
